package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsBrandMapper;
import com.qf.pojo.DtsBrand;
import com.qf.pojo.DtsCategory;
import com.qf.service.DtsBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DtsBrandServiceImpl implements DtsBrandService {
    @Autowired
    DtsBrandMapper brandMapper;
    @Override
    public List<Integer> finById(Integer id) {

        LambdaQueryWrapper<DtsBrand> qw = new LambdaQueryWrapper<>();
        qw.eq(DtsBrand::getAdminId,id);
        qw.select(DtsBrand::getId);
        List<DtsBrand> dtsBrands = brandMapper.selectList(qw);
        List<Integer> collect = dtsBrands.stream().map(dtsBrand -> {
            return dtsBrand.getId();
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public IPage<DtsBrand> findByPage(Integer page, Integer limit, String sort, String order, Integer id, String name) {
        IPage<DtsBrand> iPage = new Page<>(page,limit);
        QueryWrapper<DtsBrand> qw = new QueryWrapper<>();
        if (!StringUtils.isEmpty(id)){
            qw.eq("id",id);
        }
        if (!StringUtils.isEmpty(name)){
            qw.like("name",name);
        }
        if(!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)){
            if(order.equalsIgnoreCase("desc")) {
                qw.orderByDesc(sort);
            }else {
                qw.orderByAsc(sort);
            }
        }
        brandMapper.selectPage(iPage,qw);
        return iPage;
    }

    @Override
    public List<Map<String, Object>> findBrand() {
        LambdaQueryWrapper<DtsBrand> qw = new LambdaQueryWrapper<>();
        qw.eq(DtsBrand::getDeleted,false);
        List<DtsBrand> dtsBrands = brandMapper.selectList(qw);
        List<Map<String, Object>> data1 = dtsBrands.stream().map(dtsBrand -> {
            Map<String, Object> data = new HashMap<>();
            data.put("value", dtsBrand.getId());
            data.put("label", dtsBrand.getName());
            return data;
        }).collect(Collectors.toList());

        return data1;
    }
}
